﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using YinfangWeb.BLL;
using YinfangWeb.BLL.Enum;
using YinfangWeb.BLL.Model;
using YinfangWeb.MLog;
using YinfangWeb.Model;

namespace YinfangWeb.Api.Controllers
{
    public class OrderController : ApiController
    {
        #region 赠品加入到购物车
        /// <summary>
        /// 赠品加入到购物车
        /// </summary>
        /// <param name="token"></param>
        /// <param name="identity"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidToken]
        public IHttpActionResult AddOrderCar(string token,int identity,[FromBody]JObject obj)
        {
            dynamic data = obj;
            if (data == null) return Json(new Result(ResultCode.ParameterMissing));
            try
            {
                OrderCart orderCar = data.ToObject<OrderCart>();
                var ut = new UserBO().GetUserInfoByToken(token);
                var result = new OrderBO().AddOrderCar(ut,orderCar);
                return Json(result);
            }
            catch (Exception ex)
            {
                Logger.Default.Error("赠品加入到购物车出错！", ex);
                return Json(new Result(ResultCode.Busy, ex.Message));
            }
        }
        #endregion
        #region 获取购物车商品列表
        [HttpPost]
        [ValidToken]
        public IHttpActionResult GetOrderCarList(string token, int identity, [FromBody]JObject obj)
        {
            //dynamic data = obj;
            //if (data == null) return Json(new Result(ResultCode.ParameterMissing));
            try
            {
                //OrderCart orderCar = data.ToObject<OrderCart>();
                var ut = new UserBO().GetUserInfoByToken(token);
                var result = new OrderBO().GetOrderCarList(ut.UserDataID);
                return Json(result);
            }
            catch (Exception ex)
            {
                Logger.Default.Error("获取购物车商品列表出错！", ex);
                return Json(new Result(ResultCode.Busy, ex.Message));
            }
        }
        #endregion
    }
}
